package com.icow.wms.basic.controller;

import java.util.ArrayList;
import java.util.List;

import com.icow.wms.basic.domain.Vehicle;
import com.icow.wms.basic.utils.AgvCallLevel;
import com.icow.wms.basic.utils.AgvState;
import com.icow.wms.basic.utils.OpenTcs;
import org.springframework.cache.Cache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.AgvInfo;
import com.icow.wms.basic.service.IAgvInfoService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * agvInfoController
 *
 * @author bcs
 * @date 2024-08-26
 */
@RestController
@RequestMapping(value = {"/wcs/agvInfo", "/wcs"})
public class AgvInfoController extends BaseController {
    @Autowired
    private IAgvInfoService agvInfoService;
    @Resource(name = "agvCache")
    private Cache agvCache;

    /**
     * 查询agvInfo列表
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgvInfo agvInfo) {
        startPage();
        List<Vehicle> vehicles = OpenTcs.getVehicles();
        List<AgvInfo> list = new ArrayList<>();
        vehicles.forEach(vehicle -> {
            AgvInfo temp = new AgvInfo();
            temp.setAgvName(vehicle.getName());
            temp.setAgvState(AgvState.agvStateMap.get(vehicle.getState()));
            temp.setOrderName(vehicle.getTransportOrder() == null ? "无" : vehicle.getTransportOrder());
            temp.setCurrentPosition(vehicle.getCurrentPosition() == null ? "无" : vehicle.getCurrentPosition());
            temp.setEnergy(vehicle.getEnergyLevel() + "");
            temp.setCallLevel(AgvCallLevel.agvCallLevelMap.get(vehicle.getIntegrationLevel()));
            temp.setAgvType(vehicle.getAllowedOrderTypes().toString().replace("[", "").replace("]", ""));
            agvCache.put(vehicle.getName(), temp);
            list.add(temp);
        });
        return getDataTable(list);
    }

    @GetMapping("/show/{agvName}")
    public void show(@PathVariable String agvName) {
        OpenTcs.markVehicleIntegrationLevel(agvName, AgvCallLevel.TO_BE_RESPECTED);
    }

    @GetMapping("/dispatcher/{agvName}")
    public void dispatcher(@PathVariable String agvName) {
        OpenTcs.markVehicleIntegrationLevel(agvName, AgvCallLevel.TO_BE_UTILIZED);
    }

    @GetMapping("/cancel/{orderName}")
    public void cancel(@PathVariable String orderName) {
        if (orderName != null && !"无".equals(orderName)) {
            OpenTcs.cancelTransportOrder(orderName);
        }
    }

    /**
     * 导出agvInfo列表
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:export')")
    @Log(title = "agvInfo", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgvInfo agvInfo) {
        List<AgvInfo> list = agvInfoService.selectAgvInfoList(agvInfo);
        ExcelUtil<AgvInfo> util = new ExcelUtil<AgvInfo>(AgvInfo.class);
        return util.exportExcel(list, "agvInfo");
    }

    /**
     * 获取agvInfo详细信息
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agvInfoService.selectAgvInfoById(id));
    }

    /**
     * 新增agvInfo
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:add')")
    @Log(title = "agvInfo", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgvInfo agvInfo) {
        return toAjax(agvInfoService.insertAgvInfo(agvInfo));
    }

    /**
     * 修改agvInfo
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:edit')")
    @Log(title = "agvInfo", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgvInfo agvInfo) {
        return toAjax(agvInfoService.updateAgvInfo(agvInfo));
    }

    /**
     * 删除agvInfo
     */
    @PreAuthorize("@ss.hasPermi('wcs:agvInfo:remove')")
    @Log(title = "agvInfo", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agvInfoService.deleteAgvInfoByIds(ids));
    }
}